#include "gofgame/dungeonkeeper/menuscene/MenuLayer.h"
#include "gofgame/dungeonkeeper/game/LevelScene.h"
#include "gofgame/dungeonkeeper/game/GameManager.h"
#include "gofgame/common/IconvUnit.h"
#include "gofgame/dungeonkeeper/mapscene/MapScene.h"
#include "gofgame/dungeonkeeper/game/PlayerInfo.h"
using namespace gofgame::dungeonkeeper::menuscene;
using namespace gofgame::dungeonkeeper::mapscene;
using namespace gofgame::dungeonkeeper::game;
using namespace gofgame::common;

bool MenuLayer::init() {
	bool bRet = false;
	do 
	{
		CCSize  winSize = CCDirector::sharedDirector()->getWinSize();
		CC_BREAK_IF(!CCLayer::init());
		//BG
		CCSprite *bg = CCSprite::create("ui/menu_bg.png");
		bg->setScaleX(winSize.width/bg->getContentSize().width);
		bg->setScaleY(winSize.height/bg->getContentSize().height);
		bg->setPosition(ccp(winSize.width * 0.5,winSize.height * 0.5));
		this->addChild(bg);
		//BG BRODER;
		CCSprite *br1 = CCSprite::create("ui/menu_1.png");//ZS
		CCSprite *br2 = CCSprite::create("ui/menu_2.png");//S
		CCSprite *br3 = CCSprite::create("ui/menu_3.png");//YS
		CCSprite *br4 = CCSprite::create("ui/menu_4.png");//Y
		CCSprite *br5 = CCSprite::create("ui/menu_5.png");//YX
		CCSprite *br6 = CCSprite::create("ui/menu_6.png");//X
		CCSprite *br7 = CCSprite::create("ui/menu_7.png");//ZX
		CCSprite *br8 = CCSprite::create("ui/menu_8.png");//z

		this->addChild(br1);
		this->addChild(br2);
		this->addChild(br3);
		this->addChild(br4);
		this->addChild(br5);
		this->addChild(br6);
		this->addChild(br7);
		this->addChild(br8);

		//menu broder
		CCSprite *menuBroderLeft = CCSprite::create("ui/menu_9.png");
		CCSprite *menuBorderRight = CCSprite::create("ui/menu_9.png");
		menuBroderLeft->setPosition(ccp(530,500));
		menuBorderRight->setPosition(ccp(750,500));
		this->addChild(menuBorderRight);
		this->addChild(menuBroderLeft);

		//bgmenu

		CCMenuItemImage *game = CCMenuItemImage::create("ui/start.png","ui/start_2.png",this,menu_selector(MenuLayer::onGameClieck));
		CCMenuItemImage *setting = CCMenuItemImage::create("ui/setting.png","ui/setting_2.png",this,menu_selector(MenuLayer::onSettingClieck));
		CCMenuItemImage *exitGame = CCMenuItemImage::create("ui/cancel.png","ui/cancel_2.png",this,menu_selector(MenuLayer::onExitGameClieck));

		CCMenu *menu = CCMenu::create(game,setting,exitGame,nullptr);
		menu->setPosition(ccpAdd(menu->getPosition(),ccp(0,100)));
		menu->alignItemsVerticallyWithPadding(30.0f);
		this->addChild(menu);

		bRet = true;
	} while (0);
	return bRet;
}

void  MenuLayer::onGameClieck(CCObject *psender){
	MapScene *mp = MapScene::create();
	
	CCDirector::sharedDirector()->pushScene(mp);
}

void  MenuLayer::onSettingClieck(CCObject *psender){
	LevelScene *ls = LevelScene::createWithLevel("level1.xml");
	GameManager::sharedGameManager()->setLevelScene(ls);
	CCDirector::sharedDirector()->replaceScene(ls);
}

void  MenuLayer::onExitGameClieck(CCObject *psender){
	CCDirector::sharedDirector()->end();
}